<template>
  <!--已发布的课程-->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseSubscribeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="课程名称" align="center" prop="courseName"/>
      <el-table-column label="课程创建时间" align="center" prop="courseCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.courseCreateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否收费" align="center" prop="collectFee">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.collect_fee" :value="scope.row.collectFee"/>
        </template>
      </el-table-column>
      <el-table-column label="收费金额(元)" align="center" prop="collectFeeMoney">
        <template slot-scope="scope">
          <span>{{ scope.row.collectFeeMoney.toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程报名时间" align="center" prop="registrationTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.registrationTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程截止报名时间" align="center" prop="registrationDeadline" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.registrationDeadline }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRead(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="cancelRelease(scope.row)"
          >取消发布
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--    查看团课信息-->
    <el-dialog title="团课详情" :visible.sync="dialogTableVisible">
      <el-descriptions class="margin-top" :column="2" border>
        <el-descriptions-item>
          <template slot="label">
            团课名称
          </template>
          {{ courseInfoData.courseName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            团课描述
          </template>
          {{ courseInfoData.courseDescribe }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            团课创建时间
          </template>
          {{ courseInfoData.courseCreateTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            活动最大报名人数
          </template>
          {{ courseInfoData.registrationNumber }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            至少报名人数
          </template>
          {{ courseInfoData.registrationNumberLeast }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            是否收费
          </template>
          <el-tag v-if="courseInfoData.collectFee == '0'" type="danger">收费</el-tag>
          <el-tag v-else type="success">免费</el-tag>
        </el-descriptions-item>
        <el-descriptions-item v-if="courseInfoData.collectFee == '0'">
          <template slot="label">
            <i class="el-icon-money"></i>
            收费金额
          </template>
          {{ courseInfoData.collectFeeMoney }}元
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            开始时间
          </template>
          {{ courseInfoData.courseStartTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            结束时间
          </template>
          {{ courseInfoData.courseEndTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            报名时间
          </template>
          {{ courseInfoData.registrationTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            截止报名时间
          </template>
          {{ courseInfoData.registrationDeadline }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            图片
          </template>
          <el-carousel width="100px" v-if="courseInfoData.coverPic">
            <el-carousel-item
              v-for="item in courseInfoData.coverPic.split(',')"
              :key="item"
            >
              <!--              <ImagePreview :src="String(item)"></ImagePreview>-->
              <ImagePreview :src="String(item)"></ImagePreview>
            </el-carousel-item>
          </el-carousel>
          <el-tag v-else type="info">暂无数据</el-tag>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <!--    作废发布，说明作废原因-->
    <el-dialog :visible.sync="cancelReleaseOpen" @close="close" width="500px" height="400px" append-to-body>
      <el-form ref="elForm" v-model="cancelCause" :rules="rules" size="medium" label-position="top" label-width="200px">
        <el-form-item label="取消原因" prop="cancelCause">
          <el-input v-model="cancelCause" type="textarea" placeholder="请输入取消原因" :maxlength="100"
                    :rows="8"></el-input>
        </el-form-item>
        提示: <span style="color: #f39191">取消后无法进行发布，若需要，请重新创建团课</span>
      </el-form>
      <div slot="footer" class="button-container">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {listCourseSubscribe, updateCancelRelease} from "@/api/subscribe/releaseCourse";

export default {
  name: "releaseCourse",
  dicts: ['collect_fee', 'course_subscribe_type'],
  data() {
    return {
      //作废团课ID
      courseId: 0,
      //作废原因
      cancelCause: '',
      //作废原因弹窗
      cancelReleaseOpen: false,
      dialogTableVisible: false,
      courseInfoData: {},
      delImageIds: [],
      imageUrl: "",
      imageFile: File,
      imgUrl: process.env.VUE_APP_IMG_BASE_URL,
      //存储图片数组
      fileList: [],
      coverPic: '',//课程图片
      // 租户标识
      venueUniqueIdentifier: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程信息表格数据
      courseSubscribeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        //团课的状态为已发布
        courseStatus: 0,
      },
      // 表单参数
      form: {},
      //表单校验
      rules: {
        cancelCause: [{
          validator: (rule, value, callback) => {
            callback(new Error('请输入作废原因'));
          },
          trigger: 'blur'
        }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //查看
    handleRead(row) {
      this.dialogTableVisible = true;
      this.courseInfoData = JSON.parse(JSON.stringify(row));
    },
    //作废发布操作
    cancelRelease(row) {
      this.cancelReleaseOpen = true;
      const id = row.id;
      this.courseId = id;
    },
    handleConfirm() {
      this.$modal.confirm('确认取消发布吗? 取消后无法修改！！！')
        .then(() => {
          const id = this.courseId;
          const cancelCause = this.cancelCause;
          updateCancelRelease(id, cancelCause)
            .then((res) => {
              if (res.data.data > 0) {
                this.$message({type: "success", message: "取消成功"});
                this.getList();
                this.cancelReleaseOpen = false;
              } else {
                this.$message({type: "error", message: "取消失败"});
                this.getList();
                this.cancelReleaseOpen = false;

              }
            });
        }).catch(() => {
      });
    },
    /** 查询课程信息列表 */
    getList() {
      this.loading = true;
      listCourseSubscribe(this.queryParams).then(response => {
        this.courseSubscribeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.cancelReleaseOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseName: null,
        coverPic: null,
        courseDescribe: null,
        courseCreateTime: null,
        registrationNumber: null,
        registrationNumberLeast: null,
        collectFee: null,
        collectFeeMoney: null,
        courseStatus: null,
        venueUniqueIdentifier: null,
        courseStartTime: null,
        courseEndTime: null,
        registrationDeadline: null,
        registrationTime: null,
      };
      this.fileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    close() {
      this.cancelCause = null;
    }

  }
}
</script>

<style scoped>
.button-container {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
}
</style>
